package com.ujinsung.utils.encode {

	/**
	 * @author Tomas Vorobjov
	 * @version 1.0
	 * @date 2 May 2008
	 *
	 * @file HTML.as
	 * 
	 * This class provides methods for encoding html characters
	 */
	public class HTML {

		
		// global constants

		// local constants

		// local vars	

		/********************************************
		 ************ PRIVATE METHODS ***************
		 ********************************************/	
	
		/**
		 * Auxiliary function for the regular expression replace in
		 * <code>HTML#decodeEntities()</code>. It takes the numeric value
		 * of an entity from &#<number>; and converts it into the
		 * corresponding character.
		 */
		private static function entityReplace() : String {
			return String.fromCharCode(Number(arguments[1]));
		}

		/********************************************
		 ************ PUBLIC METHODS ****************
		 ********************************************/

		/**
		 * Returns the <i>src</i> string with HTML entities converted 
		 * to their applicable characters
		 * 
		 * @param src The source string to decode
		 * 
		 * @return The source string with HTML entities converted 
		 * to their applicable characters
		 */
		public static function decodeEntities( src : String ) : String {
			return src.replace(/&#(\d+?);/ig, HTML.entityReplace);	
		}

		/**
		 * Returns the <i>src</i> string with HTML entities converted 
		 * to special characters
		 * 
		 * The translations performed are:
		 * <ul>
		 * 	<li>'&amp;amp;' becomes '&amp;' (ampersand) </li>
		 * 	<li>'&amp;quot;' becomes '"' (double quote)</li>
		 * 	<li>'&amp;#039;' (single quote) becomes '''</li>
		 * 	<li>'&amp;lt;' (less than) becomes '&lt;'</li>
		 * 	<li>'&amp;gt;' (greater than) becomes '&gt;'</li>
		 * </ul>
		 * 
		 * @return The string with HTML entities converted 
		 * to special characters
		 */
		public static function decodeSpecialChars( src : String ) : String {
			src = src.replace(new RegExp("&amp;", "g"), "&");
			src = src.replace(new RegExp("&quot;", "g"), "\"");
			src = src.replace(new RegExp("&#039;", "g"), "'");
			src = src.replace(new RegExp("&lt;", "g"), "<");
			src = src.replace(new RegExp("&gt;", "g"), ">");
			return src;
		}	

		/**
		 * Returns the <i>src</i> string with characters which have 
		 * HTML character entity equivalents translated into these entities.
		 * 
		 * The function will convert all characters which are not from
		 * the set [a-zA-Z0-9_ \t] (alpha-numeric charaters including the 
		 * underscore, space and the tab) into their html entities 
		 * equivalents.
		 * 
		 * @param src The source string to encode
		 * 
		 * @return The source string with characters which have 
		 * HTML character entity equivalents translated into these entities.
		 */
		public static function encodeEntities( src : String ) : String {
			var encoded : Array = [];
			var alphaNumericPattern : RegExp = /[a-zA-Z0-9_ \t]/;
		
			for ( var i : Number = 0, j : Number = src.length;i < j; ++i ) {
				var char : String = src.charAt(i);
				var ordChar : Number = char.charCodeAt(0);
		
				if ( isNaN(ordChar) ) {
					// If char failed to decode, leave it as char
					encoded.push(char);
				}
		        else {
					var entity : String = "&#" + ordChar + ";";
					// if char was from [a-zA-Z0-9_ \t] leave it as char,
					// otherwise replace it with entity
					encoded.push(alphaNumericPattern.test(char) ? char : entity);
				}
			}
			return encoded.join("");	
		}

		/**
		 * Returns the <i>src</i> string with special characters converted 
		 * to HTML entities.
		 * 
		 * <p>Certain characters have special significance in HTML, 
		 * and should be represented by HTML entities if they 
		 * are to preserve their meanings. This function returns 
		 * a string with some of these conversions made; the 
		 * translations made are those most useful for everyday 
		 * web programming. If you require all HTML character 
		 * entities to be translated, use <code>HTML#encodeEntities()</code>
		 * instead.<p/>
		 * <p>This function is useful in preventing user-supplied text from
		 * containing HTML markup, such as in a message board or guest book
		 * application.</p>
		 * 
		 * The translations performed are:
		 * <ul>
		 * 	<li>'&amp;' (ampersand) becomes '&amp;amp;'</li>
		 * 	<li>'"' (double quote) becomes '&amp;quot;'</li>
		 * 	<li>''' (single quote) becomes '&amp;#039;'</li>
		 * 	<li>'&lt;' (less than) becomes '&amp;lt;'</li>
		 * 	<li>'&gt;' (greater than) becomes '&amp;gt;'</li>
		 * </ul>
		 * 
		 * @return The string with special characters converted 
		 * to HTML entities
		 */
		public static function encodeSpecialChars( src : String ) : String {
			src = src.replace(new RegExp("&", "g"), "&amp;");
			src = src.replace(new RegExp("\"", "g"), "&quot;");
			src = src.replace(new RegExp("'", "g"), "&#039;");
			src = src.replace(new RegExp("<", "g"), "&lt;");
			src = src.replace(new RegExp(">", "g"), "&gt;");
			return src;
		}		
	}
}